using System;
using System.Collections;
using System.Collections.Generic;
namespace fuzzyKittenVNC
{
	public class ArgParser
	{
		protected ArrayList rules = new ArrayList();
		protected string description;
		public ArgParser () {}

		public void addArg(string _short, string _long, string type,string desc) {
			ArrayList args = new ArrayList();
			args.Add(_short);
			args.Add(_long);
			args.Add(type);
			args.Add(desc);
			rules.Add(args);
		}
		protected Dictionary<string,string> paramlist = new Dictionary<string, string>();
		public string getParam(string key,bool required) {
			if(paramlist.ContainsKey(key))
				return paramlist[key];
			if(required)
				throw new ArgumentException(String.Format("Argument {0} is missing",key));
			return "";
		}
		public Dictionary<string,string> getParamlist() {
			return this.paramlist;
		}
		public void setDescription(string s) {
			this.description = s;
		}
		public string getDescription() {
			return this.description;
		}
		
		public void parse(string[] a) {
			bool isLong;
			for(int i=0;i<a.Length;i++) {
				if(a[i].StartsWith("-") == false)
					continue;
				string param;
				if(a[i][1] == '-') {
					isLong = true;
					param = a[i].Substring(2);
				} else {
					isLong = false;
					param = a[i].Substring(1);
				}
				bool found = false;
				foreach(ArrayList rule in this.rules) {
					if((string) rule[isLong ? 1 : 0] != param)
						continue;
					found = true;
					switch((string) rule[2]) {
						case "bool":
							paramlist[(string) rule[1]] = "1";
							break;
						case "string":
							paramlist[(string) rule[1]] = a[++i];
							break;
						
					}
				}
//				if(!found)
//					Console.Error.WriteLine("Argument "+param+" is unknown");
			}
		}
		
		public void printusage() {
			Console.WriteLine(this.description);
			Console.WriteLine("\nArguments: ");
			foreach(ArrayList rule in this.rules) {
				Console.WriteLine("-{0}|--{1}{2}\t\t\t{3}",(string)rule[0],(string)rule[1],(string)rule[2] != "bool" ? "=" : "",(string)rule[3]);
			}
			
		}
	}
}

